require("../../public/css/common.css")
require("../../public/css/normalize.css")
require("../../libs/fonts/iconfont.css")
require("./login.css")


// 引入axios
const axios = require("axios");
// 页面加载完成之后,给登录添加点击事件,获取input的value值,提交到后台
window.addEventListener("DOMContentLoaded",function(){
// 获取input
let input=document.querySelector("#input")
let btn=this.document.querySelector("button")
btn.addEventListener("click",function(){
    login()
})
input.addEventListener("keydown",function(e){
    if(e.keyCode===13){
        login()
    }
})

function login(){
        let tel = document.querySelector("#input").value
        let password = document.querySelector(".passWord").value
        let hint = document.querySelector(".hint")
        if( !/^1[3-9]\d{9}$/.test(tel) ){
            hint.innerHTML = "手机号码格式不对"
            return 
        }
        if( password.trim().length === 0 ){
            hint.innerHTML = "密码不能为空"
            return
        }

        axios.post("http://47.96.154.185:3701/api/user/login",{
            "account":tel,
            "password":password
        }).then( re=>{
            // 判断是否登录成功
            let respData = re.data
            console.log(respData);
            if( respData.errno === 0 ){
                // 需要将当前登录用户的唯一凭证放到localStorage中
                localStorage.setItem("token",respData.data.token)
                // 跳转到首页
                location.href = "./index.html"
            }else{
                msgTips.textContent = "账号或密码错误"
                return
            }
        })
        .catch(function(err){
            console.log(err);
        })
}



































})